<?php
namespace app\index\controller;

use think\Db;
use app\common\controller\HomeBase;
use app\common\model\Title as TitleModel;
use app\common\model\Imgcategory as ImgcategoryModel;

class Title extends HomeBase
{
    protected $title_model;

    protected function _initialize(){
        parent::_initialize();
        $this->title_model = new TitleModel();
        $this->imgcategory_model = new ImgcategoryModel();
    }

    public function title($id = '' ,$sid = '' , $keyword = '', $sort = 0 ,$type = 0,$page = 1){
        $category = [];
        $map = [
            'examine' => ['eq',1],
            'type' => ['eq',$type],
            'status' => ['eq',1],
        ];


        if ($sort == 0) {
            $order = 'readding DESC,create_time DESC';
        }else if($sort == 1){
            $order = 'id DESC';
        } else if ($sort == 2) {
            $order = 'readding DESC';
        } 
        // else if ($sort == 3) {
        //     $order = 'readding DESC';
        // }
         else {
            $order = 'readding DESC,create_time DESC';
        }

        if(!empty($keyword)){
            $map['title'] = ['like','%'.$keyword.'%'];
            // $img_list = $this->images_model->where($map)->order($order)->paginate(20,false,['page' => $page]);
        }else{
            if(empty($id)) $id = 76;

            if(!empty($sid)){
                $this->imgcategory_model->where('id',$sid)->setInc('click');
            }else{
                $sid = $this->imgcategory_model->where('pid',$id)->value('id');
            }
            $category = $this->imgcategory_model->where('pid',$id)->select();
            $title_list = $this->title_model->where($map)->where('pid',$sid)->order($order)->paginate(20,false,['page'=>$page]);
        }

        $this->assign('title_list',$title_list);
        $this->assign('category',$category);
        $this->assign('sort',$sort);
        $this->assign('nav',2);
        $this->assign('sid',$sid);
        $this->assign('id',$id);
        $this->assign('type',$type);
        $this->assign('keyword',$keyword);
        return $this->fetch();
    }

    /**
     * 标题页详情
     */
    public function wenantitledetail($id){
        $title_info = Db::name('title')->where('id',$id)->where('examine',1)->find();
        $this->assign('title_info',$title_info);
        return $this->fetch();
    }

    /**
     * 加载更多
     */
    public function loadMore($id , $sort ,$sid , $type , $keyword=''){
        $map = [
            'examine' => ['eq',1],
            'type' => ['eq',$type],
            'status' => ['eq',1],
            'pid' => ['eq',$sid],
        ];

        if ($sort == 0) {
            $order = 'readding DESC,create_time DESC,downloadding DESC';
        }else if($sort == 1){
            $order = 'create_time DESC';
        } else if ($sort == 2) {
            $order = 'downloadding DESC,readding DESC';
        } 
        // else if ($sort == 3) {
        //     $order = 'readding DESC';
        // }
         else {
            $order = 'readding DESC,create_time DESC,downloadding DESC';
        }

        if(!empty($keyword)){
            $map['title|abstract'] = ['like','%'.$keyword.'%'];
        }

        $result = $this->title_model->where($map)->limit($id,20)->order($order)->select();
        if($result){
            return json(['status' => 1 ,'msg' => 'ok' ,'result' =>$result]);
        }else{
            return json(['status' => -1 ,'error' => 'load more error']);
        }
    }
}